package me.jiatao.wan.paging3.footer

import android.view.View
import androidx.core.view.isVisible
import androidx.paging.LoadState
import androidx.recyclerview.widget.RecyclerView
import com.hi.dhl.binding.viewbind
import me.jiatao.wan.databinding.LayoutFooterViewHolderBinding

/**
 * @Author: JiaTao
 * @CreateDate: 2021/8/29 18:54
 * @Version: 1.0
 * @Description:
 */

class FooterViewHolder(view: View, private val retryCallback: () -> Unit) :
    RecyclerView.ViewHolder(view) {
    private val mBinding: LayoutFooterViewHolderBinding by viewbind()

    fun bindView(loadState: LoadState) {
        mBinding.apply {
            // 正在加载，显示进度条
            progress.isVisible = loadState is LoadState.Loading

            // 加载失败，显示并点击重试按钮
            retryButton.isVisible = loadState is LoadState.Error
            retryButton.setOnClickListener { retryCallback() }

            // 加载失败显示错误原因
            errorMsg.isVisible = !(loadState as? LoadState.Error)?.error?.message.isNullOrBlank()
            errorMsg.text = (loadState as? LoadState.Error)?.error?.message

            executePendingBindings()
        }
    }
}